//
//  HMTabBarItem.swift
//  Weibo
//
//  Created by 李学林 on 15/12/13.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit

class HMTabBarItem: UITabBarItem {

    override var badgeValue: String?{
        
        didSet{
            printLog("ssssss")
            
            if badgeValue == nil {
                return
            }
            
            let controller = valueForKey("_target") as! UITabBarController
             //在这个里面可以更改下系统的一些属性-----这里就直接可以更改badgeNumber的北京图片
                    for tableBarChild in controller.tabBar.subviews{
                        if tableBarChild .isKindOfClass(NSClassFromString("UITabBarButton")!){
                            for tableBarButtonChild in tableBarChild.subviews {
                                if tableBarButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!){
                                    for badgeChild in tableBarButtonChild.subviews{
                                        // badgeChild 就是我们要的东西_UIBadgeBackground
                                        printLog(badgeChild)
                                        if badgeChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")! ){
            
                                            var count: UInt32 = 0
                                            let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &count)
            
                                            // 遍历成员变量的列表
                                            for i in 0..<count {
                                                // 取到对应位置的值
                                                let ivar = ivars[Int(i)]
                                                // 获取其名字
                                                let name = ivar_getName(ivar)
                                                // 转 NSString
                                                let nameString = NSString(CString: name, encoding: NSUTF8StringEncoding)
                                                    if nameString!.isEqualToString("_image"){
            
                                                        // 利用KVC赋值,因为属性为只读属性
                                                      badgeChild.setValue(UIImage(named: "main_badge"), forKey: (nameString as! String))
            
                                                }
                                        
            
                                            }
                                        }
                                    }
                                }
                            
                            }
                        }
                        
                    
                    }
            

            
        }
    
    }
    
}
